import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/Home/components/notice_detail.dart';
import 'package:flutter_enjoy_plus/pages/Home/components/tanchuang.dart';
import 'package:flutter_enjoy_plus/pages/Login/login_page.dart';
import 'package:flutter_enjoy_plus/pages/Mine/my_name/my_xiangqing.dart';
import 'package:flutter_enjoy_plus/pages/Mine/tabs/my_home_list.dart';
import 'package:flutter_enjoy_plus/pages/tabs_page.dart';
import 'package:flutter_enjoy_plus/utils/TokenManageer.dart';

void main() {
  runApp(
    MaterialApp(
      initialRoute: '/', // 默认路由
      // 路由表
      routes: {
        '/': (context) => const TabsPage(), // 首页
        '/notice_detail': (context) => const NoticeDetail(), // 公告详情页
        '/login': (context) => const LoginPage(), // 登录页
        '/tanchuang': (context) => const TanchuangDemoPage(), // 弹窗页
      },
// 路由拦截器(路由守卫)
      onGenerateRoute: (RouteSettings settings) {
        // 获取 token 判断是否登录
        final token = tokenManager.getToken();
        // 判断是否有 token，无 token 跳转到登录页
        if (token.isEmpty) {
          // 无 token 跳转到登录页
          return MaterialPageRoute(builder: (context) => const LoginPage());
        } else {
          // 有 token 判断继续判断需要进什么页面
          if (settings.name == '/profile') {
            return MaterialPageRoute(
              builder: (context) => ProfilePage(
                userInfo: settings.arguments as Map<String, dynamic>,
              ),
            );
          }
          if (settings.name == '/house') {
            return MaterialPageRoute(builder: (context) => const HouseList());
          }
        }
        return null;
      },
    ),
  );
}
